import time
from ipymol import viewer
from ipymol import MolViewer
from typing import Literal, Optional


def restart_pymol(app: int = 4,
                  quiet: Literal['q', 'Q', ''] = 'q') -> MolViewer:
    '''重启远程pymol窗口
    
    Params
    ======
    application: pymol视窗样式,使用pymol -h查看
    quiet:'q'取消启动消息;'Q'禁用全部消息;''不禁用
    '''

    viewer.stop()
    while viewer._process_is_running():
        time.sleep(1)
    viewer.start([f'-A{app}{quiet}'])

    # 强制使用pymol色彩空间,适合网页显示
    viewer.space('pymol')
    viewer.bg_color('black')
    return viewer

def pymol_super_rmsd(mobile:str, target: str) -> float:
    '''进行结构重叠,打印并返回移动结构与目标结构之间的RMSD,使用super计算.
    
    Params
    ======
    - mobile: 移动结构的对象名(或selection)
    - target: 目标结构的对象名(或selection)
    
    Returns
    =======
    计算的RMSD,单位为埃.
    """
    '''
    super_rmsd=viewer.super(mobile,target)[0]
    print(f"pymol计算的rmsd:{super_rmsd}")
    return super_rmsd